﻿using System;
using System.Collections.Generic;
using System.Text;
using EFFC.Net.Interface;

namespace EFFC.Net.Returns
{
    [Serializable]
    public class ReturnStd : IStandardData, IReturn
    {
        protected Dictionary<string, ReElement> _p = new Dictionary<string, ReElement>();

        protected void AddReElement(string name, Type t)
        {
            if (!this._p.ContainsKey(name))
            {
                this._p.Add(name, new ReElement(name, t));
            }
        }
        protected void AddReElement(string name)
        {
            if (!this._p.ContainsKey(name))
            {
                this._p.Add(name, new ReElement(name));
            }
        }
        /// <summary>
        /// 檢查是否含有name的參數定義
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool ContainName(string name)
        {
            return _p.ContainsKey(name);
        }

         /// <summary>
        /// 设定参数值
        /// </summary>
        /// <param name="p"></param>
        /// <param name="v"></param>
        public void SetValue(string p,object v)
        {
            if (_p.ContainsKey(p))
            {
                _p[p].Value = v;
            }
            else
            {
                AddReElement(p, v.GetType());
                _p[p].Value = v;
            }
        }

        #region IStandardData Members

        public string ToStdFormatString()
        {
            return "";
        }

        public string ToString(string format)
        {
            return "";
        }

        public StdType StdType
        {
            get { return StdType.Return; }
        }

        #endregion

        #region IParameters Members

        public string[] GetReturnList()
        {
            List<string> rtn = new List<string>();
            foreach (string s in _p.Keys)
            {
                rtn.Add(s);
            }

            return rtn.ToArray();
        }
       
        public T GetValue<T>(string p)
        {
            if (_p.ContainsKey(p))
            {
                if (typeof(T) == _p[p].PType)
                {
                    if (_p[p].Value != null)
                        return (T)_p[p].Value;
                    else
                        return default(T);
                }
                else
                {
                    return default(T);
                }
            }
            else
            {
                return default(T);
            }
        }

        #endregion

    }
}
